using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

using SBPweb.Localization;
using SBPweb.Modules.Modularity;
using SBPweb.Presentation.Windows.Forms.Controls;

using Verety.SharedMeta.DirectoryServiceMeta.Enums;

namespace SBPweb.Modules.ContactsManager.Controls
{
	public partial class RenameContactControl : ModalDockingUserControl
	{
		#region Private members

		private ContactListControl mContactListControl;
		private string mJid;
		private Regex mNickNameRegex = new Regex(@"^[A-Za-z0-9]([a-zA-Z0-9.\s_])*$");

		#endregion

		#region Constructor logic

		public RenameContactControl(ContactListControl contactListControl, string jid)
			:base(contactListControl.ParentModule)
		{
			this.Caption = LocalizationExtender.GetString("RenameContactControl");
			if (this.Parent != null)
			{
				this.Parent.Resize += new EventHandler(Parent_Resize);
			}
			LocalizationExtender.OnInvalidatedControls += new EventHandler(LocalizationExtender_OnInvalidatedControls);
			mContactListControl = contactListControl;
			mJid = jid;
			InitializeComponent();

			InitializeTextboxes();
		}

		private void InitializeTextboxes()
		{
			tbID.Text = mJid;
			tbOldNickName.Text = tbNewNickName.Text = mContactListControl.DirectoryService.Contacts.GetNickName(mJid);
		}

		#endregion

		private void Finalize()
		{
			LocalizationExtender.OnInvalidatedControls -= new EventHandler(LocalizationExtender_OnInvalidatedControls);
			this.Parent.Resize -= new EventHandler(Parent_Resize);
		}

		#region Event handlers

		private void LocalizationExtender_OnInvalidatedControls(object sender, EventArgs e)
		{
			this.Caption = LocalizationExtender.GetString("RenameContactControl");
			Parent_Resize(sender, e);
		}

		private void Parent_Resize(object sender, EventArgs e)
		{
			this.Size = this.Parent.ClientSize;
			this.btnCancel.Location = new Point(this.Width - 5 - btnCancel.Width, this.Height - 5 - btnCancel.Height);
			this.btnSave.Location = new Point(5, this.Height - 5 - btnSave.Height);
			this.tbID.Width = this.tbOldNickName.Width = this.tbNewNickName.Width = this.Width - this.tbID.Location.X;
		}

		private void tbNewNickName_TextChanged(object sender, EventArgs e)
		{
			errorProvider.SetError(tbNewNickName, string.Empty);
			if (tbNewNickName.Text.Length < 1)
			{
				errorProvider.SetError(tbNewNickName, LocalizationExtender.GetString("FieldValidationEmpty"));
				return;
			}
			if (tbNewNickName.Text.Length > IntConstants.MaxNickNameLength)
			{
				errorProvider.SetError(tbNewNickName, LocalizationExtender.GetString("FieldValidationTooLong"));
				return;
			}
			if (!mNickNameRegex.IsMatch(tbNewNickName.Text))
			{
				errorProvider.SetError(tbNewNickName, LocalizationExtender.GetString("FieldValidationInvalidCharacter"));
				return;
			}
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			tbNewNickName.Text = tbNewNickName.Text.Trim();
			tbNewNickName_TextChanged(sender, e);
			if (errorProvider.GetError(tbNewNickName) != string.Empty)
			{
				return;
			}
			if (tbNewNickName.Text != tbOldNickName.Text)
			{
				mContactListControl.DirectoryService.Contacts.SetNickName(mJid, tbNewNickName.Text);
				mContactListControl.DirectoryService.Contacts.GetContactList();
			}
			Finalize();
			ParentModule.Host.Smartparts.Remove(this);
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			Finalize();
			ParentModule.Host.Smartparts.Remove(this);
		}

		#endregion
	}
}
